//
//  QLLoginController.m
//  神约
//
//  Created by qlwl on 16/10/27.
//  Copyright © 2016年 qlwl. All rights reserved.
//

#import "QLLoginController.h"
#import "NSString+Judge.h"
#import "NSString+MD5.h"
#import "QLLoginModel.h"
#import "AppDelegate+QLDelegate.h"
#import "NSUserDefaults+MyDefaults.h"
#import "UIWindow+Extension.h"


@interface QLLoginController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet UITextField *phoneField;
@property (weak, nonatomic) IBOutlet UITextField *passwordField;

@end

@implementation QLLoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setNavBarColor:NavBarTintColor];
//    [self getMyNavBarWithNavTitle:@"立马到登录" andLeftBtnTitle:nil andLeftIma:@"return" andRightTitle:nil andRightIma:nil and:^(int tag) {
//        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
//    }];
    
    [self setNavTitle:@"登录"];

}

#pragma mark btnClick

- (IBAction)loginBtnClick:(id)sender {
    [self loginNet];
}

#pragma mark net
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void)loginNet
{
    if (![NSString validateMobile:self.phoneField.text]) {
        return;
    }
    if (!self.passwordField.text.length) {
        return;
    }
    NSMutableDictionary * postDict = [[NSMutableDictionary alloc] init];
    postDict[@"mobilephone"] = self.phoneField.text;
    postDict[@"password"] = [NSString MD5WithString:self.passwordField.text];
    [QLNetWorking postJSONWithUrl:QLLoginURL parameters:postDict isJosnRequest:YES rogress:nil success:^(id result) {
        NSLog(@"%@",result);
        if ([QLNetWorking sucess:[result objectForKey:@"mobileHead"] and:@"code" EqualToString:@"success"]) {
            QLLoginModel * model = [QLLoginModel mj_objectWithKeyValues:[[result objectForKey:@"mobileBody"] objectForKey:@"data"]];
            if ([model save]) {
                [LPPopup showInCenterOfWindowWithText:@"登录成功" completion:nil];
                [[AppDelegate keyWindow] chooseRootViewController];
            }
        }else{
            [LPPopup showInCenterOfWindowWithText:[[result objectForKey:@"mobileHead"] objectForKey:@"message"] completion:nil];
        }
    } fail:^(NSError *error) {
        
    }];
}

//- (void)loginWithUserInfo:(QLLoginModel *) loginModel
//{
//    NSString *loginAccount = loginModel.yx_accid;
//    NSString *loginToken   = loginModel.yx_token;
//    
//    //NIM SDK 只提供消息通道，并不依赖用户业务逻辑，开发者需要为每个APP用户指定一个NIM帐号，NIM只负责验证NIM的帐号即可(在服务器端集成)
//    //用户APP的帐号体系和 NIM SDK 并没有直接关系
//    //DEMO中使用 username 作为 NIM 的account ，md5(password) 作为 token
//    //开发者需要根据自己的实际情况配置自身用户系统和 NIM 用户系统的关系
//    
//    
//    [[[NIMSDK sharedSDK] loginManager] login:loginAccount
//                                       token:loginToken
//                                  completion:^(NSError *error) {
//                                      [SVProgressHUD dismiss];
//                                      if (error == nil)
//                                      {
//                                          LoginData *sdkData = [[LoginData alloc] init];
//                                          sdkData.account   = loginAccount;
//                                          sdkData.token     = loginToken;
//                                          [[NTESLoginManager sharedManager] setCurrentLoginData:sdkData];
//                                          
//                                          //上传clientId
//                                          [self upClientIdWithUserId:loginModel.userId];
//                                          // 登录成功
//                                          [LPPopup showInCenterOfWindowWithText:@"登录成功" completion:nil];
//                                          [[AppDelegate keyWindow] chooseRootViewController];
//                                          
//                                          [[NTESServiceManager sharedManager] start];
//                        
//                                      }
//                                      else
//                                      {
//                                          NSString *toast = [NSString stringWithFormat:@"登录失败 code: %zd",error.code];
//                                          [self.view makeToast:toast duration:2.0 position:CSToastPositionCenter];
//                                      }
//                                  }];
//
//}
//
- (void)upClientIdWithUserId:(NSString *)userId
{
    NSString * clientId = [NSUserDefaults objectForKey:QLGeTuiClientId];
    if ([NSString isNull:clientId]) {
        return;
    }
    NSMutableDictionary * postDict = [[NSMutableDictionary alloc] init];
    postDict[@"id"] = userId;
    postDict[@"clientid"] = clientId;
    [QLNetWorking postJSONWithUrl:QLUpdateClientId parameters:postDict isJosnRequest:YES rogress:nil success:^(id result) {
        
    } fail:^(NSError *error) {
        
    }];
}

@end
